package com.yellowpage.omc.ability.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.omc.ability.dao.PartnerParamValueDao;
import com.yellowpage.omc.ability.data.PartnerParamValue;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class PartnerParamValueServiceImpl extends BaseServiceImpl implements PartnerParamValueService {
	  private PartnerParamValueDao partnerParamValueDao;
	
	  public void setPartnerParamValueDao(PartnerParamValueDao partnerParamValueDao) {
		  this.partnerParamValueDao = partnerParamValueDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public PartnerParamValue getPartnerParamValue(Long id){
	      return partnerParamValueDao.get(id);
	  }
	  
    public List<PartnerParamValue> getAllPartnerParamValue(){
        return partnerParamValueDao.getAll();
    }
    
    public List<Map> queryPartnerParamValueByPage(Map queryMap, PageRender pageRender) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT c.NAME as PARTNER_NAME, d.NAME as INTERFACE_NAME, e.PARAM_NAME, b.PARTNER_ID, b.INTERFACE_ID, " +
				"       a.PARTNER_PARAM_VALUE_ID , a.INTERFACE_PARAM_ID , a.PARAM_VALUE_ID , a.PARAM_VALUE , a.NOTE ," +
				"       (select display_name from CA_USER where user_id = a.LAST_UPDATE_USER) as last_update_user, " +
				"       (select display_name from CA_USER where user_id = a.CREATE_USER) as create_user, " +
				"        a.CREATE_TIME , a.LAST_UPDATE_TIME , a.STATUS , (SELECT display_name FROM CA_USER WHERE user_id = a.LAST_SYNCHRONIZATION_USER) AS last_synchronization_user,a.LAST_SYNCHRONIZATION_TIME " +
				"  FROM OMC_PARTNER_PARAM_VALUE a, OMC_PARTNER_INTERFACE b, " +
				"       OMC_PARTNER c,  OMC_INTERFACE d, OMC_INTERFACE_PARAM e  " +
				" WHERE a.PARTNER_INTERFACE_ID = b.PARTNER_INTERFACE_ID " +
				"   and b.PARTNER_ID = c.PARTNER_ID    " +
				"   and b.INTERFACE_ID = d.INTERFACE_ID  " +
				"   and a.INTERFACE_PARAM_ID = e.INTERFACE_PARAM_ID  ")
				.addCondition(" and b.PARTNER_ID =  :partnerId ")
				.addCondition(" and b.INTERFACE_ID = :interfaceId ")
				    			.addCondition(" and PARTNER_PARAM_VALUE_ID = :partnerParamValueId ")
							    			.addCondition(" and INTERFACE_PARAM_ID = :interfaceParamId ")
							    			.addCondition(" and PARAM_VALUE_ID = :paramValueId ")
							    			.addCondition(" and PARAM_VALUE = :paramValue ")
							    			.addCondition(" and NOTE = :note ")
							    			.addCondition(" and CREATE_USER = :createUser ")
							    		    .addCondition(" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
		    				    			.addCondition(" and LAST_UPDATE_USER = :lastUpdateUser ")
							    		    .addCondition(" and date_format(LAST_UPDATE_TIME,'%Y-%m-%d') =  :lastUpdateTime  ")
		    				    			.addCondition(" and STATUS = :status ")
		    				    			.addCondition(" and LAST_SYNCHRONIZATION_USER = :LastSynchronizationUser ")
							    			.addCondition(" and LAST_SYNCHRONIZATION_TIME = :LastSynchronizationTime ")
							    ; 		    
		    

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
    
    public String getPartnerNameByPartnerInterfaceId(Long partnerInterfaceId) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"select c.NAME as partner_name, d.NAME as interface_name" +
				"  from OMC_PARTNER_INTERFACE b," +
				"       OMC_PARTNER c," +
				"       OMC_INTERFACE d" +
				"  where b.PARTNER_ID = c.PARTNER_ID" +
				"    and b.INTERFACE_ID = d.INTERFACE_ID" +
				"     and b.PARTNER_INTERFACE_ID =  " + partnerInterfaceId) ; 		    
		    

		List<Map> resultList = sqlAssistant.query(
				baseDao.getConnection(), new HashMap());
		
		String partnerName = "";
		
		if(resultList.size() > 0){
			partnerName = (String)resultList.get(0).get("PARTNER_NAME");		
		}

		return partnerName;
	}
    
    public String getInterfaceNameByPartnerInterfaceId(Long partnerInterfaceId) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"select c.NAME as partner_name, d.NAME as interface_name" +
				"  from OMC_PARTNER_INTERFACE b," +
				"       OMC_PARTNER c," +
				"       OMC_INTERFACE d" +
				"  where b.PARTNER_ID = c.PARTNER_ID" +
				"    and b.INTERFACE_ID = d.INTERFACE_ID" +
				"     and b.PARTNER_INTERFACE_ID =  " + partnerInterfaceId) ;    
		    

		List<Map> resultList = sqlAssistant.query(
				baseDao.getConnection(), new HashMap());
		
		String interfaceName = "";
		
		if(resultList.size() > 0){
			interfaceName = (String)resultList.get(0).get("INTERFACE_NAME");		
		}

		return interfaceName;
	}
    
    
    
    public void savePartnerParamValue(PartnerParamValue partnerParamValue){
        partnerParamValueDao.save(partnerParamValue);
    }
    
	  public void updatePartnerParamValue(PartnerParamValue partnerParamValue){
	      partnerParamValueDao.update(partnerParamValue);
	  }
	  
	  public void delete(List deleteList){
	      partnerParamValueDao.remove(deleteList);
	  }
	  
	  
	 
	
}	